#include "tnotifywindow.h"
#include "iostream"

TNotifyWindow::TNotifyWindow(QString Title, QString Text, int pos, QWidget *parent) :
    QWidget (parent),
    thisObject (this),
    message (new QLabel(this)),
    desktop (QApplication::desktop()),
    timer (new QTimer()),
    toolBarHeight (55),
    animation (new QSequentialAnimationGroup())
{
    this->setWindowFlags(Qt::ToolTip | // This type flag is the second point
                           Qt::FramelessWindowHint |
                           Qt::WindowSystemMenuHint |
                           Qt::WindowStaysOnTopHint |
                           Qt::X11BypassWindowManagerHint);

    this->setAttribute(Qt::WA_TranslucentBackground);

    //Editing title
    QFontMetrics fm(message->font());
    Title = fm.elidedText(Title,Qt::ElideRight,285);
    Title.insert(0,"<b>");
    Title.append("</b><br>");

    Text.insert(0, QString("<font color=white>").append(Title));
    Text.append("</font>");
    message->setStyleSheet( "QLabel {padding: 10px;"\
                            "padding-left: 20px;"\
                            "padding-right: 20px;"\
                            "margin: 2x;"\
                            "border: 0px solid white;"\
                            "border-radius: 15px;"\
                            "background-color: rgba(0%,0%,0%,80%);}" );

    message->setText(Text);
    std::cout << Text.toStdString() << std::endl;
    message->setMinimumWidth(150);
    message->setMaximumWidth(825);
    message->setMinimumHeight(40);
    message->setMaximumHeight(350);
    message->setWordWrap(true);
    message->adjustSize();
    timer->setSingleShot(true);
    QSettings settings;
    settings.beginGroup("notifications");
    int timeout = settings.value("timeout").toInt();
    settings.endGroup();
    timer->setInterval(timeout);
    timer->start();
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(sendRequestDelete()));
    QObject::connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(sendRequestDelete()));
    message->show();
    this->resize(message->size());
    std::cout << this->width() << ":" << this->height() << std::endl;
    std::cout << message->width() << ":" << message->height() << std::endl;

    this->move(desktop->width(), desktop->height() - this->height() - pos - toolBarHeight);
    targetRect = this->geometry();
    this->show();

    QObject::connect(animation, SIGNAL(finished()), this, SLOT(clearAnimation()));
}

void TNotifyWindow::sendRequestDelete(void){
    emit requestDelete(thisObject);
}

void TNotifyWindow::showEvent(QShowEvent * event){
    animationList.append(new QPropertyAnimation(this, "geometry"));
    animationList.last()->setDuration(300);
    animationList.last()->setStartValue(targetRect);
    targetRect.adjust(-this->width(), 0, -this->width(),0);
    animationList.last()->setEndValue(targetRect);
    animation->addAnimation(animationList.last());
    if (animation->state() != QAbstractAnimation::Running){
        animation->start();
    }
}

void TNotifyWindow::clearAnimation(void){
    animationList.clear();
    animation->clear();
}

void TNotifyWindow::animateDown(int pixels){
    animationList.append(new QPropertyAnimation(this, "geometry"));
    animationList.last()->setDuration(300);
    animationList.last()->setStartValue(targetRect);
    targetRect.adjust(0,pixels,0,pixels);
    animationList.last()->setEndValue(targetRect);
    animation->addAnimation(animationList.last());
    if (animation->state() != QAbstractAnimation::Running){
        animation->start();
    }
}

void TNotifyWindow::animateHide(void){
    animationList.append(new QPropertyAnimation(this, "geometry"));
    animationList.last()->setDuration(300);
    animationList.last()->setStartValue(targetRect);
    //std::cout << targetRect.x() << ":" << targetRect.y() << ":" << targetRect.width() << ":" << targetRect.height() << std::endl;
    targetRect.adjust(this->width(), 0, this->width(),0);
    //std::cout << targetRect.x() << ":" << targetRect.y() << ":" << targetRect.width() << ":" << targetRect.height() << std::endl;
    animationList.last()->setEndValue(targetRect);
    QObject::connect(animationList.last(), SIGNAL(finished()), this, SLOT(deleteLater()));
    animation->addAnimation(animationList.last());
    if (animation->state() != QAbstractAnimation::Running){
        animation->start();
    }
}
